package com.comet.controller;

import com.comet.domain.Course;
import com.comet.domain.Order;
import com.comet.service.OrderService;
import com.comet.service.CourseService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/order/")
public class OrderController {

    @Resource
    private CourseService courseService;

    @Resource
    private OrderService orderService;

    /**
     * 分布式事务测试
     */
    // 网关访问：http://localhost:8888/order-service/api/v1/order/gts
    // 本地访问：http://localhost:8000/api/v1/order/gts
    @RequestMapping("/gts")
    public Object createOrder() {
        Order order = new Order();
        order.setCourseId(Long.valueOf("1000000"));
        order.setCourseName("SpringCloud从入门到精通");
        order.setCourseDesc("百度技术专家带你玩转SpringCloud");
        order.setPrice(new BigDecimal(699.00));
        order.setLogoUrl("/img/course_img/sc.jpg");
        order.setUserId(Long.valueOf("2000000"));
        order.setCreateTime(new Date());
        order.setRemark("暂无备注");

        // 插入订单并扣减库存
        Object status = orderService.createOrder(order);

        return status;

    }

    /**
     * 订单服务调用视频服务
     * 模拟购买视频操作，查询视频信息
     */
    // http://localhost:8000/api/v1/order/find_by_id?videoId=40
    @RequestMapping("find_video_by_id")
    public Object findCourseById(int courseId) {
        Course course = courseService.findById(courseId);
        Order order = new Order();
        order.setOrderId(course.getCourseId());
        order.setCourseName("xxxxxxxxxxx");
        order.setCourseDesc("xxxxxx");
        return order;
    }

    /**
     * 订单服务调用视频服务传递视频对象
     * @return
     */
    // http://localhost:8000/api/v1/order/save_video
    @RequestMapping("save_video")
    public Object save(@RequestBody Course course) {
        int effectLine = courseService.save(course);
        return new HashMap<>().put("status", effectLine);
    }

    /**
     * Test
     */
    // http://localhost:8000/api/v1/order/save_video
    @RequestMapping("list")
    public Object list(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("title:","Alibaba Cloud Singleton");
        map.put("server port:",request.getServerPort()+"");
        return map;
    }

    /**
     * 根据订单ID查询订单
     * @param orderId
     */
    // http://localhost:8000/api/v1/order/find_order_by_id/100
    @GetMapping(value = "findOrderById/{orderId}")
    public Order findOrderById(@PathVariable int orderId) {
        Order order = orderService.findOrderById(orderId);
        return order;
    }

    /**
     * 流控测试：限流、熔断
     */
     int temp = 0;
    @GetMapping(value = "/sentinelFlowLimit/{query}")
    public Object apiHello(@PathVariable String query) {
        temp++;
        if (temp % 3 == 0) {
            throw new RuntimeException();
        }
        return orderService.sentinelFlowLimit(query);
    }

}
